﻿using System;
using System.Collections.Generic;
using System.Text;
using Daco.Security;

namespace Daco.Web.UI.WebControls
{
    public class MemberInfo : System.Web.UI.WebControls.WebControl, System.Web.UI.IPostBackEventHandler
    {
        public event EventHandler LogOut;

        public override string ClientID
        {
            get
            {
                return string.Format("DACO_WebControl_ProfileList_" + this.GetHashCode());
            }
        }

        public override string UniqueID
        {
            get
            {
                return string.Format("DACO$WebControl$ProfileList$" + this.GetHashCode());
            }
        }

        private ISitePage _currentPage = null;
        public MemberInfo()
        {
            this.Load += new EventHandler(MemberInfo_Load);
        }

        void MemberInfo_Load(object sender, EventArgs e)
        {
            _currentPage = CPageContext.Current.CurrentPage;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            CMembershipUser user = _currentPage.CurrentUser;
            CPermission permission = _currentPage.UserPermission;

            writer.WriteLine(@"<div class=""DIVBox"">
                            <div class=""DIVBoxHeader"">
                                <div><h4>Thành viên</h4></div>
                            </div>
                            <div class=""DIVBoxContent"">
                                <div>");

            writer.Write(@"
                <table style=""border: none;"">
                    <tr>
                        <td>Tên</td>
                        <td>{1}</td>
                    </tr>
                    <tr>
                        <td>Nhóm</td>
                        <td>{2}</td>
                    </tr>
                    <tr>
                        <td>Quyền</td>
                        <td>{3}</td>
                    </tr>
                    <tr>
                        <td colspan=""2"" align=""right""><a href=""{0}"" title=""Thoát khỏi hệ thống"">Thoát</a></td>
                    </tr>
                </table>
            ", 
            Page.ClientScript.GetPostBackClientHyperlink(this, string.Empty),
            user.UserName,
            Daco.Utils.CHelper.ArrayToString<string>(user.Roles, ','),
            permission.BitValue);

            writer.WriteLine(@"</div>
                        </div>
                    </div>");
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            OnLogOut();
        }

        private void OnLogOut()
        {
            if (LogOut != null)
                LogOut(this, new EventArgs());
        }
    }
}
